package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.Expenditure;
import com.ctgu.fcftsystem.service.ExpenditureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ExpenditureController")
public class ExpenditureController {
    @Autowired
    private ExpenditureService expenditureService;

    /**
     * 根据时间查询支出
     * @param expenditureDate
     * @return
     */
    @RequestMapping("/searchExpenditureByDate")
    public Map searchExpenditureByDate(String expenditureDate){
        if(expenditureDate==null||expenditureDate.length()==0){
            Map<String, Object> mcp = new HashMap<>();
            mcp.put("code", 1);
            mcp.put("msg", "传入参数为空");
            mcp.put("data", -1);
            return mcp;
        }
        Map<String, Object> map = new HashMap<>();

        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = format.parse(expenditureDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Expenditure> expenditureList = expenditureService.searchExpenditureByDate(date);
        if (expenditureList != null&&expenditureList.size()>0) {
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", expenditureList);
        }
        else{
            map.put("code", 1);
            map.put("msg", "所查询的结果不存在");
            map.put("data", expenditureList);
        }


        return map;
    }

    /**
     * 根据类型对支出记录进行查询
     * @param expenditureType
     * @return
     */
    @RequestMapping("/searchExpenditureByType")
    public Map searchExpenditureByType(Integer expenditureType){
       Map<String, Object> map = new HashMap<>();
       List<Expenditure> expenditureList = expenditureService.searchExpenditureByType(expenditureType);
       if(expenditureList!=null&&expenditureList.size()>0){
           map.put("code",0);
           map.put("msg","查询支出记录成功");
           map.put("data",expenditureList);

       }else{
           map.put("code",1);
           map.put("msg","查询支出记录失败");
           map.put("data",expenditureList);
       }
       return map;
    }

}
